package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result queryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询....");
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(categoryPageQueryDTO.getName() != null,"name",categoryPageQueryDTO.getName() );
        queryWrapper.eq(categoryPageQueryDTO.getType() != null,"type",categoryPageQueryDTO.getType() );
        queryWrapper.orderByAsc("sort");
        Page<Category> categoryPage = categoryService.page(new Page<>(categoryPageQueryDTO.getPage(),
                categoryPageQueryDTO.getPageSize()), queryWrapper);
        return Result.success(categoryPage);
    }

    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result saveCgy(@RequestBody CategoryDTO categoryDTO){

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        log.info("新增分类:{}" ,categoryDTO);
        categoryService.save(category);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("分类启停")
    public Result startOrStop (@PathVariable Integer status,Long id){
        log.info("启用禁用账号：{},{}",status,id);
        Category category = categoryService.getById(id);
        category.setStatus(status);
        categoryService.updateById(category);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("编辑分类")
    public Result<Category> update (@RequestBody Category category){
        log.info("编辑分类....");
        categoryService.updateById(category);
        return Result.success(category);
    }
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result remove (Category category){
        log.info("删除分类....");
        categoryService.removeById(category);
        List<Dish> list = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, category.getId())
                .list();
        Long count = setMealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, category.getId())
                .count();
        if(list.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("分类下拉框")
    public Result<List> CategorySelect( Category category){
        List<Category> list = categoryService.lambdaQuery()
                .eq(category.getType() != null, Category::getType, category.getType())
                .orderByDesc(Category::getSort)
                .list();
        return Result.success(list);

    }



}
